<template>
  <div class="table-box">
    <KingTable ref="kingTable" :tableOptions="_tableOptions" @handle-btn-click="handleBtnAction">
      <!-- 表格操作 -->
      <template #operation="scope">
        <TableRowBtns :row="scope.row" @handle-row-action="handleBtnAction" />
      </template>
    </KingTable>
    <add ref="addRef" @fetch-data="fetchTableData" />
    <edit ref="editRef" @fetch-data="fetchTableData" />
    <info ref="infoRef" @fetch-data="fetchTableData" />
  </div>
</template>

<script setup lang="tsx" name="ParamList">
import { ref, reactive } from "vue";
import KingTable from "@/components/KingTable/index.vue";
import TableRowBtns from "@/components/KingTable/components/TableRowBtns.vue";
import { operateType } from "@/plugins/commonType.enum";
import add from "./components/noticeAdd.vue";
import edit from "./components/noticeEdit.vue";
import info from "./components/noticeView.vue";

// 获取 KingTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const kingTable = ref();

const addRef = ref();
const infoRef = ref();
const editRef = ref();

// 如果表格需要初始化请求参数，直接定义传给 KingTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const _initParam = reactive({ EQ_ss: 1, sorts: "mts@desc" });

// 表格配置项
const _tableCols = [
  { type: "selection", fixed: "left", align: "center", width: 60 },
  { type: "index", label: "序号", align: "center", width: 60 },
  {
    prop: "noticeTitle",
    label: "公告标题",
    overflow: true
  },
  {
    prop: "cts",
    label: "发表时间",
    width: 180
  },
  {
    prop: "noticeType",
    label: "公告类别",
    dictCode: "NoticeTypes",
    width: 180
  },
  {
    prop: "status",
    label: "状态",
    width: 120,
    dictCode: "sys_normal_disable"
  },
  { prop: "operation", label: "操作", fixed: "right", width: 260 }
];

const _searchCols = [
  {
    prop: "EQ_noticeType",
    label: "参数类型",
    width: 100,
    el: "select",
    dictCode: "NoticeType"
  },
  {
    prop: "LIKE_noticeTitle",
    label: "通知标题",
    width: 100,
    el: "input"
  }
];

const _tableOptions = {
  title: "通知公告",
  queryUrl: "system/notice/query",
  operUrl: "system/notice",
  tableColumns: _tableCols,
  searchColumns: _searchCols,
  initParam: _initParam
};

/**
 * 增删查按钮执行的业务操作
 * @param {*} type 当前执行操作的类型
 * @param {*} row
 */
const handleBtnAction = async (type: string, row: any) => {
  switch (type) {
    case operateType.ADD: // 新增操作
      addRef.value.showDialog(row, type);
      break;
    case operateType.EDIT: // 编辑操作
      editRef.value.showDialog(row, type);
      break;
    case operateType.DETAIL: // 查询操作
      infoRef.value.showDialog(row, type);
      break;
    case operateType.DEL: // 删除操作
      kingTable.value?.handleDelete(row);
      break;
    case operateType.BATCH_DEL: // 批量删除操作
      kingTable.value?.handleBatchDelete();
      break;
  }
};

// 重新拉取表格数据
const fetchTableData = () => {
  kingTable.value?.getTableList();
};
</script>
